package cn.net.autocode.platform.customDict.dict;

import cn.hutool.core.util.StrUtil;
import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.platform.customDict.CustomDict;
import cn.net.autocode.platform.customDict.CustomDictAnnotation;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@CustomDictAnnotation("tableFields")
@Component("autocode_platform_tableFields")
public class TableFields implements CustomDict {

    private final Dao dictDao;

    public TableFields(@Qualifier("dictDao") Dao dictDao) {
        this.dictDao = dictDao;
    }

    @Override
    public ResultT query(JSONObject param) {
        String tableName = param.getString("tableName");
        if(StrUtil.isNotBlank(tableName)){
            JSONArray array = dictDao.queryJSONArray("select t1.name value,t1.name label from ac_table_fields t1,ac_table t2 where t1.table_id=t2.id and t2.table_name=? order by t1.name",tableName);
            return ResultT.success(array);
        }else{
            JSONArray array = dictDao.queryJSONArray("select name value,name label from ac_table_fields where table_id=? order by name",param.getString("tableId"));
            return ResultT.success(array);
        }
    }
}
